package com.ruoyi.web.controller.siyun;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SyAppUsers;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.siyun.domain.SyAppBlacklist;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.siyun.service.ISyAppUsersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app用户Controller
 * 
 * @author ruoyi
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/users/users")
public class SyAppUsersController extends BaseController
{
    @Autowired
    private ISyAppUsersService syAppUsersService;

    /**
     * 查询app用户列表
     */
    @PreAuthorize("@ss.hasPermi('users:users:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyAppUsers syAppUsers)
    {
        startPage();
        List<SyAppUsers> list = syAppUsersService.selectSyAppUsersList(syAppUsers);
        return getDataTable(list);
    }

    /**
     * 导出app用户列表
     */
    @PreAuthorize("@ss.hasPermi('users:users:export')")
    @Log(title = "app用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SyAppUsers syAppUsers)
    {
        List<SyAppUsers> list = syAppUsersService.selectSyAppUsersList(syAppUsers);
        ExcelUtil<SyAppUsers> util = new ExcelUtil<SyAppUsers>(SyAppUsers.class);
        util.exportExcel(response, list, "app用户数据");
    }

    /**
     * 获取app用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('users:users:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(syAppUsersService.selectSyAppUsersById(id));
    }

    /**
     * 新增app用户
     */
    @PreAuthorize("@ss.hasPermi('users:users:add')")
    @Log(title = "app用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SyAppUsers syAppUsers)
    {
        // 设置创建者
        syAppUsers.setCreateBy(getUsername());
        syAppUsers.setPasswordHash(SecurityUtils.encryptPassword(syAppUsers.getPasswordHash()));
        syAppUsers.setSalt(SecurityUtils.encryptPassword(syAppUsers.getPasswordHash(),syAppUsers.getUsername()));
        return toAjax(syAppUsersService.insertSyAppUsers(syAppUsers));
    }


    /**
     * 修改app用户
     */
    @PreAuthorize("@ss.hasPermi('users:users:edit')")
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyAppUsers syAppUsers)
    {
        // 检查是否需要加密密码
//        if (StringUtils.isNotEmpty(syAppUsers.getPasswordHash())) {
//            syAppUsers.setPasswordHash(SecurityUtils.encryptPassword(syAppUsers.getPasswordHash()));
//        }
        // 密码加盐
//        syAppUsers.setSalt(SecurityUtils.encryptPassword(syAppUsers.getPasswordHash(),syAppUsers.getUsername()));

        return toAjax(syAppUsersService.updateSyAppUsers(syAppUsers));
    }


    /**
     * 删除app用户
     */
    @PreAuthorize("@ss.hasPermi('users:users:remove')")
    @Log(title = "app用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(syAppUsersService.deleteSyAppUsersByIds(ids));
    }



    /**
     * 修改app用户状态
     */
    @PreAuthorize("@ss.hasPermi('users:users:changeStatus')")
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SyAppUsers syAppUsers)
    {
        // 设置更新者
        syAppUsers.setUpdateBy(getUsername());
        // 调用服务方法更新状态
        return toAjax(syAppUsersService.updateSyAppUsersStatus(syAppUsers));
    }
    /**
     * 重置app用户密码
     */
    @PreAuthorize("@ss.hasPermi('users:users:resetPwd')")
    @Log(title = "app用户", businessType = BusinessType.UPDATE)
    @PutMapping("/resetAppPwd")
    public AjaxResult resetAppPwd(@RequestBody SyAppUsers syAppUsers)
    {
//        syAppUsers.setPasswordHash(SecurityUtils.encryptPassword(syAppUsers.getPasswordHash()));
        syAppUsers.setUpdateBy(getUsername());
        return toAjax(syAppUsersService.resetAppPwd(syAppUsers));
    }

    /**批量插入黑名单记录
     * @param
     * @return 操作结果
     */
//    @PreAuthorize("@ss.hasPermi('users:users:insertBlacklist')")
//    @Log(title = "app用户", businessType = BusinessType.UPDATE)
//    @PutMapping("/insertBlacklist")
//    public AjaxResult insertSyAppBlacklist(@RequestBody SyAppBlacklist syAppBlacklist) {
//        try {
//            syAppUsersService.insertSyAppBlacklist(syAppBlacklist);
//            return AjaxResult.success("黑名单记录插入成功");
//
//        } catch (Exception e) {
//            logger.error("插入黑名单记录失败", e);
//            return AjaxResult.error("插入黑名单记录失败: " + e.getMessage());
//        }
//    }
    @PostMapping("/insertBlacklist")
    public AjaxResult insertBlacklist(@RequestBody SyAppBlacklist syAppBlacklist) {
        try {
            int result = syAppUsersService.insertBlacklistByAppUserId(syAppBlacklist);
            if (result > 0) {
                return AjaxResult.success("黑名单记录添加成功");
            } else {
                return AjaxResult.error("黑名单记录添加失败");
            }
        } catch (Exception e) {
            logger.error("添加黑名单记录失败", e);
            return AjaxResult.error("添加黑名单记录失败: " + e.getMessage());
        }
    }


}
